package de.g18.llmaa.gui.workspace;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;

import de.g18.llmaa.gui.IWorkspace_Beobachter_Component;
import de.g18.llmaa.gui.workspace.internalFrame.Abstract_InternalFrame;
import de.g18.llmaa.logic.properties.PropertiesAndIconLoader;

/**
 * Das Anzeige Element des Workspace Models {@link IWorkspace_Model}, Das
 * element wird von veänderungen im Model Informiert und Stellt diese da.
 * 
 * @author Gruppe LLMAA
 * 
 */
public class Workspace_Desktop extends JDesktopPane implements
		IWorkspace_Beobachter_Component {

	private JLabel backgroundLabel;
	private Image iconBackground;
	
	 private Paint bkground;

	
	public Workspace_Desktop(){
		try {
            BufferedImage x = ImageIO.read(new File("./cfg/"+PropertiesAndIconLoader.getRuntimeProperties().getProperty("desktop.background")));
            
            bkground = new TexturePaint(x, new Rectangle(0,0,x.getWidth(), x.getHeight()));
        } catch (IOException e) {
            e.printStackTrace();
        }
//	iconBackground = (PropertiesAndIconLoader.getInstance().image_Background).getImage();
			
	
	
	}
	protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (bkground != null) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(bkground);
            
            g2.fillRect(0,0,getWidth(), getHeight());
        }
	}
//	public void paintComponent( Graphics g )
//	{
//	   if(iconBackground == null)
//	      super.paintComponent(g);
//	   else
//	   {
//	      Graphics2D g2d = (Graphics2D)g;
//
//	      //scale the image to fit the size of the Panel
//	      double mw = iconBackground.getWidth(null);
//	      double mh = iconBackground.getHeight(null);
//
//	      double sw = getWidth() / mw;
//	      double sh = getHeight() / mh;
//
//	      g2d.scale(sw, sh);
//	      g2d.drawImage(iconBackground, 0, 0, this);
//	   }
//	}
	
	@Override
	public void refresh_NewInternalFrame(Abstract_InternalFrame internalFrame) {

		this.add(internalFrame);
		internalFrame.setVisible(true);

	}

	@Override
	public void refresh_All(IWorkspace_Model model) {
		removeAll();
		for (Abstract_InternalFrame internalFrame : model
				.getList_VisibleFrames()) {
			add(internalFrame);
		}

		updateUI();

	}

	@Override
	public JComponent getComponent() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void refresh_RemoveInternalFrame(Abstract_InternalFrame internalFrame) {
		this.remove(internalFrame);
		updateUI();
	}

}
